package com.fxkj.cloud.drms.infra.repository;

import com.fxkj.cloud.drms.domain.commandSettingHistory.CommandSettingHistoryDomain;
import com.fxkj.cloud.drms.domain.commandSettingHistory.repository.CommandSettingHistoryDomainRepository;
import com.fxkj.cloud.drms.infra.converter.CommandSettingHistoryConverter;
import com.fxkj.cloud.drms.infra.persistence.dao.CommandSettingHistoryJpaRepository;
import com.fxkj.cloud.drms.infra.persistence.po.CommandSettingHistoryPO;
import com.fxkj.common.service.BaseDomainRepositoryImpl;
import jakarta.persistence.criteria.Predicate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.context.ApplicationEventPublisher;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>description: </p>
 *
 * @author ww
 * @date 2025-03-25 11:06:40
 */
@Component
public class CommandSettingHistoryDomainRepositoryImpl extends BaseDomainRepositoryImpl<CommandSettingHistoryDomain, CommandSettingHistoryPO, CommandSettingHistoryConverter> implements CommandSettingHistoryDomainRepository {

    private final CommandSettingHistoryJpaRepository commandSettingHistoryJpaRepository;
    private final CommandSettingHistoryConverter commandSettingHistoryConverter;

    public CommandSettingHistoryDomainRepositoryImpl(ApplicationEventPublisher eventPublisher, CommandSettingHistoryJpaRepository commandSettingHistoryJpaRepository, CommandSettingHistoryConverter commandSettingHistoryConverter) {
        super(eventPublisher);
        this.commandSettingHistoryJpaRepository = commandSettingHistoryJpaRepository;
        this.commandSettingHistoryConverter = commandSettingHistoryConverter;
    }

    @Override
    protected CommandSettingHistoryJpaRepository getRepository() {
        return commandSettingHistoryJpaRepository;
    }

    @Override
    protected CommandSettingHistoryConverter getConverter() {
        return commandSettingHistoryConverter;
    }

    @Override
    public Page<CommandSettingHistoryDomain> page(Pageable page, UUID deviceId, Integer attributeType, Long startTime, Long endTime, String deviceName, String username, UUID organizationId) {
        Specification<CommandSettingHistoryPO> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();  // 新增条件集合
            if (null != deviceId) {
                predicates.add(criteriaBuilder.equal(root.get("deviceId"), deviceId));
            }
            if (null != attributeType) {
                predicates.add(criteriaBuilder.equal(root.get("attributeType"), attributeType));
            }
            if (null != startTime) {
                predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createdTime"), startTime));
            }
            if (null != endTime) {
                predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("createdTime"), endTime));
            }
            if (null != deviceName) {
                predicates.add(criteriaBuilder.like(root.get("deviceName"), "%" + deviceName + "%"));
            }
            if (null != username) {
                predicates.add(criteriaBuilder.like(root.get("username"), "%" + username + "%"));
            }
            if (null != organizationId) {
                predicates.add(criteriaBuilder.equal(root.get("organizationId"), organizationId));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));  // 返回所有条件的组合
        };
        PageRequest pageRequest = PageRequest.of(page.getPageNumber(), page.getPageSize(), Sort.by(Sort.Order.desc("createdTime")));
        return commandSettingHistoryConverter.toDomainPage(commandSettingHistoryJpaRepository.findAll(specification, pageRequest));
    }

    @Override
    public List<CommandSettingHistoryDomain> getLatestCommandSettingHistoryKv(UUID deviceId, List<String> attributeKeys, UUID organizationId) {
        return commandSettingHistoryConverter.toDomainList(commandSettingHistoryJpaRepository.getLatestCommandSettingHistoryKv(deviceId, attributeKeys));
    }
}
